﻿using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Comunication
{
    public class Command
    {
        public string Header { get; set; }
        public Commands Cmd { get; set; }
        public int DataLength { get; set; }
        public string Data { get; set; }
        
        public virtual byte[] ToByteArray()
        {
            UTF8Encoding encoder = new UTF8Encoding();

            byte[] bytesHeader = encoder.GetBytes(Header);
            byte[] bytesCommand = encoder.GetBytes(((int)Cmd).ToString().PadLeft(2, '0'));
            byte[] data = encoder.GetBytes(Data);
            byte[] dataLength = encoder.GetBytes(data.Length.ToString().PadLeft(5, '0'));

            IEnumerable<byte> bytes = bytesHeader.Concat(bytesCommand).Concat(dataLength).Concat(data);
            return bytes.ToArray();
        }
    }
}
